//
//  BrowserCommit.m
//  gdemm
//
//  Created by 乔明超 on 16/11/30.
//  Copyright © 2016年 Goldencis. All rights reserved.
//

#import "BrowserCommit.h"
#import "UrlInfo.h"
#import "FMDB.h"
#import "HttpUtil.h"
#import "ApiSupport.h"
#import "KeyChain.h"
@implementation BrowserCommit

+(instancetype) shareInstance
{
    static BrowserCommit * _instance = nil;
    static dispatch_once_t onceToken ;
    dispatch_once(&onceToken, ^{
        _instance = [[self alloc] init] ;
    }) ;
    
    return _instance ;
}

- (void)commitHistory{

    @synchronized (self) {
        
        //获取Gview浏览器的数据库路径
        NSFileManager *fileMgr = [NSFileManager defaultManager];
        NSURL *url = [fileMgr containerURLForSecurityApplicationGroupIdentifier:@"group.cn.neiwang.gview"];
        NSString *path = [[url path] stringByAppendingPathComponent:@"profile.profile/browser.db"];
        FMDatabase *db = [FMDatabase databaseWithPath:path];
        
        if ([db open]) {

            FMResultSet *result = [db executeQuery:@"select * from history where should_upload = 1;"];
            while ([result next]) {
                NSString *title = [result objectForColumnName:@"title"];
                NSString *url = [result objectForColumnName:@"url"];
                NSString *domain = [NSURL URLWithString:url].host;
                NSInteger time = [result longLongIntForColumn:@"local_modified"];
                NSDate *date = [NSDate dateWithTimeIntervalSince1970:time/1000];
        
                //实例化一个NSDateFormatter对象
                NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
                //设定时间格式,这里可以设置成自己需要的格式
                [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
                //用[NSDate date]可以获取系统当前时间
                NSString *currentDateStr = [dateFormatter stringFromDate:date];
                //输出格式为：2010-10-27 10:22:13
                NSMutableDictionary *dict = [NSMutableDictionary dictionary];
                //获取参数
                NSString *userGuid = [[UserDefaultsUtil shareInstance]getString:LC_USERGUID];
                [dict setValue:userGuid forKey:@"userGUID"];
                [dict setValue:[KeyChain getUDID] forKey:@"serialNum"];
                [dict setValue:title forKey:@"browseTitle"];
                [dict setValue:url forKey:@"browseUrl"];
                [dict setValue:domain forKey:@"siteDomain"];
                [dict setValue:@1 forKey:@"siteType"];
                [dict setValue:currentDateStr forKey:@"operTime"];
                NSDictionary *resultDict = [HttpUtil postWithUrl:[ApiSupport browsingBehaviorURL] withDict:dict withType:@"argv"];
                NSString *guidStr = [result objectForColumnName:@"guid"];
                
                [db executeUpdate:@"update history set should_upload = 0 where guid = ?",guidStr];
                HRLog(@"guid = %@ domain = %@",guidStr,domain);
                HRLog(@"resultDict = %@ title = %@ - url = %@ - time = %@ - %ld",resultDict,title,url,currentDateStr,time);
            }
        
    }
}
}
@end
